package com.ttbj.product.api.impl;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.google.gson.JsonObject;
import com.ttbj.constant.SMSReminderEnum;
import com.ttbj.core.util.JsonTools;
import com.ttbj.core.util.SendSMSUtil;
import com.ttbj.product.api.ReminderFacade;
import com.ttbj.product.bean.dto.DriverDto;
import com.ttbj.util.Assert;

@Service("reminderFacade")
public class ReminderFacadeImpl implements ReminderFacade {
	public static final Logger logger = LoggerFactory.getLogger(ReminderFacadeImpl.class);

 
	private void recieverOrderSMSProcess(String jsonObject,String phone){
		try {
			SendSMSUtil.sendSms(phone,jsonObject, SMSReminderEnum.RECEVIER_ORDER_REMINDER.signName, SMSReminderEnum.RECEVIER_ORDER_REMINDER.templateCode);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	
	/**
	 * xx师傅，联系电话：xx地址xx到xx兔兔搬家订单请登陆app查收！
	 */
	private String dispatchRemindJsonJson (String driverName,String senderAddr,String receiverAddr,String senderPhone ){
		logger.info("driverName:{},senderAddr:{},receiverAddr:{},senderPhone:{}",driverName,senderAddr,receiverAddr,senderPhone);
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("driverName", driverName);
		jsonObject.addProperty("senderAddr", senderAddr);
		jsonObject.addProperty("receiverAddr", receiverAddr);
		jsonObject.addProperty("senderPhone", senderPhone);
		return JsonTools.GsonString(jsonObject); 
	}

	@Override
	public void dispatchRemind(List<DriverDto> list, String reciverAddr, String senderAddr, String senderPhone) {
		if(Assert.isEmpty(list)){
			logger.info("短信联系人有误");
			return;
		}
		if(Assert.isEmpty(reciverAddr)||Assert.isEmpty(senderAddr)||Assert.isEmpty(senderPhone)){
			logger.info("短信信息有误");
			return;
		}
		list.stream().filter(driver->!Assert.isEmpty(driver.getDriverPhone())).forEach(driver->recieverOrderSMSProcess(dispatchRemindJsonJson(driver.getDriverRealName(), senderAddr, reciverAddr, senderPhone), driver.getDriverPhone()));
	}
	
	

}
